<?php
/**
 * Created by PhpStorm.
 * User: G
 * Date: 2021/3/27
 * Time: 下午3:25
 */

namespace app\api\command;

use app\admin\model\profit\Config;
use app\admin\model\profit\ProfitDetail;
use app\admin\model\wanlshop\OrderGoods;
use app\api\model\wanlshop\Order;
use app\api\model\wanlshop\Pay;
use app\api\model\wanlshop\Shop;
use app\common\model\User;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Exception;

/*
 * 订单流水分润 定时任务
 */

class OrderShare extends Command
{

    /**
     * 名称描述配置
     * @author G
     * @date 2021-03-27 下午4:08
     */
    protected function configure()
    {
        $this->setName('order_share')->setDescription('Order Share Command');
    }

    /**
     * 分润处理
     * @param Input $input
     * @param Output $output
     * @return int|null|void
     * @author G
     * @date 2021-03-27 下午5:25
     */
    protected function execute(Input $input, Output $output)
    {
        // 查询确认收货 未分润的订单
        $un_share_orders = Order::where(['state' => 4, 'is_share' => 0])->select();
        if (!$un_share_orders) {
            echo '暂无数据';exit;
        }

        // 获取分润配置 8 : 流水分润（百分比）
        $profit_config_8 = Config::getConfigById(8);
        // 获取分润配置 10 : 直推收益（百分比）
        $profit_config_10 = Config::getConfigById(10);

        foreach ($un_share_orders as $k => $v) {
            // 查询店铺信息
            $shop_info = Shop::where('id', $v['shop_id'])->find();
            if (!$shop_info) {
                echo '暂无店铺信息'; continue;
            }

            // 店铺商家id（用户id）
            $user_info = User::where('id', $shop_info['user_id'])->find();

            // 订单商品金额
            $order_price_info = Pay::where(['order_id' => $v['id']])->find();
            $order_price = $order_price_info['actual_payment'];

            // 推荐人 分佣 = 区域合伙人 分佣 = 订单金额的 0.1%
//            $referrer_price = $c_price = bcmul($order_price, 0.001, 2);
            $referrer_price = bcmul($order_price, $profit_config_8['referrer_price'] / 100, 2);
            $p_price = bcmul($order_price, $profit_config_8['p_price'] / 100, 2);
            // 市级合伙人 分佣 = 订单金额的 0.2%
//            $p_price = bcmul($order_price,0.002, 2);
            $c_price = bcmul($order_price,$profit_config_8['c_price'] / 100, 2);

            // 总分佣 = 推荐人 分佣 + 区域合伙人 分佣 + 市级合伙人 分佣
            $t_price = bcadd(bcadd($referrer_price, $p_price, 2), $c_price, 2);

            $temp_data = [
                'uid' => $user_info['id'],
                'referrer_uid' => $user_info['referrer_uid'],
                'p_uid' => $user_info['p_uid'],
                'c_uid' => $user_info['c_uid'],
                'referrer_price' => $referrer_price,
                'p_price' => $p_price,
                'c_price' => $c_price,
                't_price' => $t_price,
                'print' => 8,   //方案8
                'creat_time' => time(),
                'update_time' => time()
            ];

            // 插入分润表
            $insert_detail_res = ProfitDetail::insert($temp_data);
            if (!$insert_detail_res) {
                throw new Exception('保存分润信息失败');
            }

            // 购买人直推分润 @date：2021-08-13
            $buyer_user_info = User::where(['id' => $v['user_id']])->find();
            if ($buyer_user_info['referrer_uid']) {
//                $buyer_referrer_price =  bcmul($order_price, 0.001, 2);
                $buyer_referrer_price =  bcmul($order_price, $profit_config_10['referrer_price'], 2);
                // 如果有推荐人，增加直推分润
                $insert_buyer_profit = ProfitDetail::insert([
                    'uid' => $buyer_user_info['id'],
                    'referrer_uid' => $buyer_user_info['referrer_uid'],
                    'p_uid' => 0,
                    'c_uid' => 0,
                    'referrer_price' => $buyer_referrer_price,
                    'p_price' => 0,
                    'c_price' => 0,
                    't_price' => $buyer_referrer_price,
                    'print' => 10,   // 方案10
                    'creat_time' => time(),
                    'update_time' => time()
                ]);

                if (!$insert_buyer_profit) {
                    throw new Exception('保存直推分润信息失败');
                }
            }

            // 修改订单分润状态
            $update_order_res = Order::where('id', $v['id'])->update([
                'is_share' => 1,
                'updatetime' => time(),
            ]);
            if (!$update_order_res) {
                throw new Exception('修改订单分润状态失败');
            }

            echo "成功";
        }
    }
}